<?
require_once('ValidatorBase.php');

class StringLengthValidator extends ValidatorBase {
	private $trim,$min,$max;

	// ACCESSORS
	public function setTrim($trim) { $this->trim= $trim ? true : false; return $this; }
	public function getTrim() { return $this->trim; }
	public function setMin($count) { $this->min= $count; return $this; }
	public function getMin() { return $this->min; }
	private function validateMin($string) {
		if (is_null($this->min)) return true;
		return strlen($string)>= $this->min;
	}

	public function setMax($count) { $this->max= $count; return $this; }
	public function getMax() { return $this->max; }
	private function validateMax($string) {
		if (is_null($this->max)) return true;
		return strlen($string)<= $this->max;
	}

	protected function doValidate($value) {
		$check= $this->getTrim() ? trim($value) : $value;
		if (
			!is_string($value) ||
			!$this->validateMin($check) ||
			!$this->validateMax($check)
		) {
			throw new ValidatorException($this,$check);
		}
	}
}
